<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div class="container">
        <div class="left">
            <span>原理图与源代码</span>
            <img src="../img/2.png" alt="">
            <div class="code">
                <pre class="precode">
 public class Input {
    private ArrayList<String> lineTxt = new ArrayList<String>();

    public ArrayList<String> getLineTxt() {
        return lineTxt;
    }

    public void input(String fileName) {
        BufferedReader inputFile = null;
        try {
            inputFile = new BufferedReader(new FileReader(fileName));
        } catch (Exception e) {
            e.printStackTrace();
        }
        String line;
        try {
            while ((line = inputFile.readLine()) != null) {
                lineTxt.add(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

public class Shift {
    private ArrayList<String> kwicList = new ArrayList<String>();
    private ArrayList<String> lineTxt;

    public Shift( ArrayList<String> lineTxt) {
        this.lineTxt = lineTxt;
    }

    public ArrayList<String> getKwicList() {
        return kwicList;
    }


    public ArrayList<String> getLineTxt() {
        return lineTxt;
    }


    public void shift() {
        //获取每个单词，存入tokens
        Iterator<String> it = lineTxt.iterator();
        while (it.hasNext()) {
            StringTokenizer token = new StringTokenizer(it.next());
            ArrayList<String> tokens = new ArrayList<String>();
            int i = 0;
            //循环添加单词
            int count = token.countTokens();
            while (i < count) {
                tokens.add(token.nextToken());
                i++;
            }

            //display(tokens);
            //切割各个单词，不断改变起始值和利用loop实现位移。
            for (i = 0; i < count; i++) {
                StringBuffer lineBuffer = new StringBuffer();
                int index = i;
                for (int f = 0; f < count; f++) {
                    //从头继续位移
                    if (index >= count)
                        index = 0;
                    //存入StringBuffer
                    lineBuffer.append(tokens.get(index));
                    lineBuffer.append(" ");
                    index++;
                }
                String tmp = lineBuffer.toString();
                kwicList.add(tmp);
            }
        }

    }
}

public class Alphabetizer {
    private ArrayList<String> kwicList;

    public Alphabetizer(ArrayList<String> kwicList) {
        this.kwicList = kwicList;
    }

    public ArrayList<String> getKwicList() {
        return kwicList;
    }

    public void sort() {
        Collections.sort(this.kwicList, new AlphabetizerComparator());
    }

    private class AlphabetizerComparator implements Comparator<String> {
        @Override
        public int compare(String o1, String o2) {
            if (o1 == null && o2 == null) {
                throw new NullPointerException();
            }
            int compareValue = 0;
            char o1c = o1.toLowerCase().charAt(0); //忽略大小写
            char o2c = o2.toLowerCase().charAt(0); //忽略大小写
            compareValue = o1c - o2c;
            return compareValue;

        }

    }
}
public class Output {
    private ArrayList<String> kwicList;
    private BufferedWriter outputFile;

    public Output(ArrayList<String> kwicList) {
        this.kwicList = kwicList;
    }

    public void output(String filename){
        Iterator<String> it = kwicList.iterator();
        try {
            outputFile = new BufferedWriter(new FileWriter(filename));
            while (it.hasNext()) {
                outputFile.write(it.next()+"\n");
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            try {
                if (outputFile!=null) {
                    outputFile.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
public class Main {
    public static void main(String[] args) {
        Input input = new Input();
        input.input("E:\\input.txt");
        Shift shift = new Shift(input.getLineTxt());
        shift.shift();
        Alphabetizer alphabetizer = new Alphabetizer(shift.getKwicList());
        alphabetizer.sort();
        Output output = new Output(alphabetizer.getKwicList());
        output.output("E:\\output.txt");

    }
}

</pre>
            </div>
        </div>
        <div class="right">
            <span>文件上传</span>
    <input type="file" id="inputFile" accept=".txt">
    <button onclick="processFile()">Process File</button>
            <div class="original">
                <span>你的文本</span>
                <div id="fileContent"></div></div>
                <span>修改过后</span>
            <div class="changed"><pre id="output"></pre></div>
        </div>
    </div>
</body>
<style>
    .container {
        display: flex;
        justify-content:space-around;
    }
    .right,.left {
        width: 600px;
        display: flex;
        flex-direction: column;
        align-items: center;
        border: 1px solid rgb(134, 134, 138);
        border-radius: 5px;
    }
   .left>span {
        font-size: 20px;
        font-weight: bolder;
    }
    .code {
        width: 500px;
    }
    .precode {
        background-color: #dcddd9;
    }
.original span{
    font-size: 20px;
}
</style>
<script>
    // 获取文件输入元素
    var input = document.getElementById('inputFile');
    // 获取用于显示文件内容的元素
    var fileContent = document.getElementById('fileContent');

    // 为文件输入元素添加'change'事件监听器
    input.addEventListener('change', function(e) {
        var files = e.target.files; // 获取文件列表
        if (files.length) {
            var file = files[0]; // 获取单个文件
            var reader = new FileReader(); // 创建FileReader对象

            // 为FileReader对象添加'load'事件监听器
            reader.onload = function(e) {
                // e.target.result 包含了文件的内容
                fileContent.textContent = e.target.result;
            };

            // 读取文件内容
            reader.readAsText(file);
        }
    });
     function processFile() {
    const input = document.getElementById('inputFile').files[0];
    if (input) {
        const reader = new FileReader();
        reader.onload = function(e) {
            const text = e.target.result;
            const kwicProcessor = new KWICProcessor();
            kwicProcessor.input(text);
            kwicProcessor.shift();
            kwicProcessor.alphabetizer();
            document.getElementById('output').textContent = kwicProcessor.output();
        };
        reader.readAsText(input);
    }
}

class KWICProcessor {
    constructor() {
        this.kwicList = [];
        this.lineTxt = [];
    }

    input(text) {
        this.lineTxt = text.split('\n');
    }

    shift() {
        this.lineTxt.forEach(line => {
            const tokens = line.split(/\s+/);
            for (let i = 0; i < tokens.length; i++) {
                let lineBuffer = [];
                for (let f = 0; f < tokens.length; f++) {
                    let index = (i + f) % tokens.length;
                    lineBuffer.push(tokens[index]);
                    if (f < tokens.length - 1) lineBuffer.push(" ");
                }
                this.kwicList.push(lineBuffer.join(""));
            }
        });
    }

    alphabetizer() {
        this.kwicList.sort((a, b) => {
            const lowerA = a.toLowerCase();
            const lowerB = b.toLowerCase();
            return lowerA.localeCompare(lowerB);
        });
    }

    output() {
        return this.kwicList.join('\n');
    }
}
</script>
</html>